/**
 * 
 */
package dms.presentation.action.global;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import dms.model.Building;
import dms.model.Dormitory;
import dms.model.role.Student;
import dms.model.util.PageIterator;
import dms.service.BuildingService;
import dms.service.DormitoryService;
import dms.service.StudentQueryService;
import dms.util.ContextUtil;

/**
 * @author 李健
 *
 */
public class GlobalAjaxEventAction extends MappingDispatchAction {

	private static Log log = LogFactory.getLog(GlobalAjaxEventAction.class);
	
	/**
	 * 检查寝室是否存在，并返回json对象
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	public ActionForward checkDormitoryExist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		Long buildingId = null;
		String dormitoryName = null;
		Building building = null;
		Dormitory dormitory = null;
		Map<Object, Object> map = new HashMap<Object, Object>();
		try {
			buildingId = Long.valueOf(request.getParameter("building"));
			dormitoryName = request.getParameter("dormitoryName");
			building = buildingService.findById(buildingId);
			
			dormitory = dormitoryService.findByBuildingAndName(building, dormitoryName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (dormitory == null) {
			map.put("exist", false);
			map.put("message", "你要找到寝室不存在！");
		} else {
			map.put("exist", true);
			map.put("message", dormitory.getDescription());
		}
		
		JSONObject jsonObject = this.createJSONObject(map);
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		log.info(jsonObject.toString());
		return null;
		
	}
	
	/**
	 * 获得某寝室的所有学生信息并返回json对象
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getStudentListByDormitory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		StudentQueryService studentQueryService = (StudentQueryService) ContextUtil.getContext().getBean("studentQueryService");
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		
		Building building = null;
		Dormitory dormitory = null;
		PageIterator<Student> students = null;
		Map<Object, Object> jsonMap = new HashMap<Object, Object>();
		List<String> stuNames = new ArrayList<String>();
		JSONObject jsonObject = null;
		try {
			Long buildingId = Long.valueOf(request.getParameter("building"));
			String dormitoryName = request.getParameter("dormitoryName");
			building = buildingService.findById(buildingId);
			
			dormitory = dormitoryService.findByBuildingAndName(building, dormitoryName);
			students = studentQueryService.findByDormitory(dormitory, 0, 10);
			
			for (Student stu : students.getList()) {
				stuNames.add(stu.getRealName());
			}
			jsonMap.put("students", stuNames);
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonMap.put("status", false);
			jsonMap.put("message", e.getMessage());
		}
		jsonObject = createJSONObject(jsonMap);
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		log.info(jsonObject.toString());
		
		return null;
	}
	
	private JSONObject createJSONObject(Map<Object, Object> map) {
		return JSONObject.fromObject(map);
	}
	
	
}
